package cc.rengu.igas.channel.esb.biz.service.impl;

import cc.rengu.igas.channel.esb.common.constant.EsbServiceConstant;
import cc.rengu.igas.channel.esb.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.esb.facade.GatewayPayService;
import cc.rengu.igas.channel.esb.facade.request.*;
import cc.rengu.igas.channel.esb.facade.response.*;
import cc.rengu.igas.channel.esb.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by wangcheng
 * 网关支付
 *
 * @author wangcheng
 */
public class GatewayPayServiceImpl implements GatewayPayService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<GatewayDebitCardPayResponse> gatewayDebitCardPay(GatewayDebitCardPayRequest gatewayDebitCardPayRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GatewayDebitCardPayResponse> result = new Result<>();
        GatewayDebitCardPayResponse gatewayDebitCardPayResponse = new GatewayDebitCardPayResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(gatewayDebitCardPayRequest, EsbServiceConstant.GATEWAY_DEBIT_CARD_PAY_SERVICE, GatewayDebitCardPayResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GatewayDebitCardPayResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行借记卡网关支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                gatewayDebitCardPayResponse.setRespCode(((BizException) e).getCode());
                gatewayDebitCardPayResponse.setRespDesc(e.getMessage());
            } else {
                gatewayDebitCardPayResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                gatewayDebitCardPayResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<GatewayDebitCardPayStatusQueryResponse> gatewayDebitCardPayStatusQuery(GatewayDebitCardPayStatusQueryRequest gatewayDebitCardPayStatusQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GatewayDebitCardPayStatusQueryResponse> result = new Result<>();
        GatewayDebitCardPayStatusQueryResponse gatewayDebitCardPayStatusQueryResponse = new GatewayDebitCardPayStatusQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(gatewayDebitCardPayStatusQueryRequest, EsbServiceConstant.GATEWAY_DEBIT_CARD_PAY_STATUS_QUERY_SERVICE, GatewayDebitCardPayStatusQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GatewayDebitCardPayStatusQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行借记卡网关支付状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                gatewayDebitCardPayStatusQueryResponse.setRespCode(((BizException) e).getCode());
                gatewayDebitCardPayStatusQueryResponse.setRespDesc(e.getMessage());
            } else {
                gatewayDebitCardPayStatusQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                gatewayDebitCardPayStatusQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<GatewayDebitCardRefundResponse> gatewayDebitCardRefund(GatewayDebitCardRefundRequest gatewayDebitCardRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GatewayDebitCardRefundResponse> result = new Result<>();
        GatewayDebitCardRefundResponse gatewayDebitCardRefundResponse = new GatewayDebitCardRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(gatewayDebitCardRefundRequest, EsbServiceConstant.GATEWAY_DEBIT_CARD_PAY_REFUND_SERVICE, GatewayDebitCardRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GatewayDebitCardRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行借记卡网关支付退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                gatewayDebitCardRefundResponse.setRespCode(((BizException) e).getCode());
                gatewayDebitCardRefundResponse.setRespDesc(e.getMessage());
            } else {
                gatewayDebitCardRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                gatewayDebitCardRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<GatewayCreditCardPayResponse> gatewayCreditCardPay(GatewayCreditCardPayRequest gatewayCreditCardPayRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GatewayCreditCardPayResponse> result = new Result<>();
        GatewayCreditCardPayResponse gatewayCreditCardPayResponse = new GatewayCreditCardPayResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(gatewayCreditCardPayRequest, EsbServiceConstant.GATEWAY_CREDIT_CARD_PAY_SERVICE, GatewayCreditCardPayResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GatewayCreditCardPayResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行贷记卡网关支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                gatewayCreditCardPayResponse.setRespCode(((BizException) e).getCode());
                gatewayCreditCardPayResponse.setRespDesc(e.getMessage());
            } else {
                gatewayCreditCardPayResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                gatewayCreditCardPayResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<GatewayCreditCardPayStatusQueryResponse> gatewayCreditCardPayStatusQuery(GatewayCreditCardPayStatusQueryRequest gatewayCreditCardPayStatusQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GatewayCreditCardPayStatusQueryResponse> result = new Result<>();
        GatewayCreditCardPayStatusQueryResponse gatewayCreditCardPayStatusQueryResponse = new GatewayCreditCardPayStatusQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(gatewayCreditCardPayStatusQueryRequest, EsbServiceConstant.GATEWAY_CREDIT_CARD_PAY_STATUS_QUERY_SERVICE, GatewayCreditCardPayStatusQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GatewayCreditCardPayStatusQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行贷记卡网关支付状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                gatewayCreditCardPayStatusQueryResponse.setRespCode(((BizException) e).getCode());
                gatewayCreditCardPayStatusQueryResponse.setRespDesc(e.getMessage());
            } else {
                gatewayCreditCardPayStatusQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                gatewayCreditCardPayStatusQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<GatewayCreditCardRefundResponse> gatewayCreditCardRefund(GatewayCreditCardRefundRequest gatewayCreditCardRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GatewayCreditCardRefundResponse> result = new Result<>();
        GatewayCreditCardRefundResponse gatewayCreditCardRefundResponse = new GatewayCreditCardRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(gatewayCreditCardRefundRequest, EsbServiceConstant.GATEWAY_CREDIT_CARD_PAY_REFUND_SERVICE, GatewayCreditCardRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GatewayCreditCardRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行贷记卡网关支付退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                gatewayCreditCardRefundResponse.setRespCode(((BizException) e).getCode());
                gatewayCreditCardRefundResponse.setRespDesc(e.getMessage());
            } else {
                gatewayCreditCardRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                gatewayCreditCardRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
